package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IMenuService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 菜谱Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-30
 */
@Service
public class MenuServiceImpl implements IMenuService {
    @Resource
    private MenuMapper menuMapper;
    @Resource
    private MenuDetailMapper menuDetailMapper;
    @Resource
    private TypeMapper typeMapper;
    @Resource
    private SpecMapper specMapper;
    @Resource
    private BrandMapper brandMapper;

    /**
     * 查询菜谱
     *
     * @param id 菜谱主键
     * @return 菜谱
     */
    @Override
    public Menu selectMenuById(Long id) {
        Menu menu = menuMapper.selectMenuById(id);

        List<MenuDetail> menuDetails = menuDetailMapper.selectMenuDetailList(new MenuDetail() {{
            setMenuId(menu.getId());
        }});
        menuDetails.forEach(item -> {
            if (item.getTypeId() != null) {
                Type type = typeMapper.selectTypeById(item.getTypeId());
                if (type != null) {
                    item.setTypeName(type.getName());
                }
            }
            if (item.getSpecId() != null) {
                Spec spec = specMapper.selectSpecById(item.getSpecId());
                if (spec != null) {
                    item.setSpecName(spec.getName());
                }
            }
            if (item.getBrandId() != null) {
                Brand brand = brandMapper.selectBrandById(item.getBrandId());
                if (brand != null) {
                    item.setBrandName(brand.getName());
                }
            }
        });

        menu.setDetails(menuDetails);

        return menu;
    }

    /**
     * 查询菜谱列表
     *
     * @param menu 菜谱
     * @return 菜谱
     */
    @Override
    public List<Menu> selectMenuList(Menu menu) {
        return menuMapper.selectMenuList(menu);
    }

    /**
     * 查询菜谱列表_dose
     */
    @Override
    public List<Menu> selectMenuList_dose(Menu menu) {
        List<Menu> menuList = menuMapper.selectMenuList(menu);
        Map<Long, Type> typeMap = typeMapper.selectTypeList(new Type()).stream().collect(Collectors.toMap(Type::getId, t -> t));
        Map<Long, Spec> specMap = specMapper.selectSpecList(new Spec()).stream().collect(Collectors.toMap(Spec::getId, t -> t));
        Map<Long, Brand> brandMap = brandMapper.selectBrandList(new Brand()).stream().collect(Collectors.toMap(Brand::getId, t -> t));
        Map<Long, List<MenuDetail>> detailMap = menuDetailMapper.selectMenuDetailList_join(new MenuDetail()).stream().collect(Collectors.groupingBy(MenuDetail::getMenuId));
        menuList.forEach(item -> {
            if (detailMap.containsKey(item.getId())) {
                List<MenuDetail> menuDetails = detailMap.get(item.getId());
                if (menuDetails != null && !menuDetails.isEmpty()) {
                    menuDetails.forEach(detail -> {
                        if (detail.getTypeId() != null) {
                            Type type = typeMap.get(detail.getTypeId());
                            if (type != null) {
                                detail.setTypeName(type.getName());
                            }
                        }
                        if (detail.getSpecId() != null) {
                            Spec spec = specMap.get(detail.getSpecId());
                            if (spec != null) {
                                detail.setSpecName(spec.getName());
                            }
                        }
                        if (detail.getBrandId() != null) {
                            Brand brand = brandMap.get(detail.getBrandId());
                            if (brand != null) {
                                detail.setBrandName(brand.getName());
                            }
                        }
                    });
                    item.setDetails(menuDetails);
                }
            }

        });
        return menuList;
    }

    /**
     * 新增菜谱
     *
     * @param menu 菜谱
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMenu(Menu menu) {
        menu.setCreateTime(DateUtils.getNowDate());
        if (menu.getParams().get("details") == null) {
            throw new ServiceException("未添加材料");
        }
        int i = menuMapper.insertMenu(menu);

        insertBatch(menu);

        return i;
    }

    /**
     * 修改菜谱
     *
     * @param menu 菜谱
     * @return 结果
     */
    @Override
    @Transactional
    public int updateMenu(Menu menu) {
        menu.setUpdateTime(DateUtils.getNowDate());

        if (menu.getParams().get("details") == null) {
            throw new ServiceException("未添加材料");
        }

        int i = menuMapper.updateMenu(menu);
        //删除关联表
        menuDetailMapper.deleteMenuDetailByMenuId(menu.getId());

        insertBatch(menu);

        return i;
    }

    /**
     * 批量新增
     */
    private void insertBatch(Menu menu) {
        List<MenuDetail> menuDetails = JSON.parseArray(JSON.toJSONString(menu.getParams().get("details")), MenuDetail.class);
        menuDetails.forEach(item -> {
            item.setMenuId(menu.getId());
        });
        menuDetailMapper.insertMenuDetailBatch(new HashMap<String, Object>() {{
            put("details", menuDetails);
        }});
    }

    /**
     * 批量删除菜谱
     *
     * @param ids 需要删除的菜谱主键
     * @return 结果
     */
    @Override
    public int deleteMenuByIds(Long[] ids) {
        int i = menuMapper.deleteMenuByIds(ids);
        menuDetailMapper.deleteMenuDetailByMenuIds(ids);
        return i;
    }

    /**
     * 删除菜谱信息
     *
     * @param id 菜谱主键
     * @return 结果
     */
    @Override
    public int deleteMenuById(Long id) {
        return menuMapper.deleteMenuById(id);
    }
}
